﻿using System;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.HU;
using HiStaff.Util;
using HiStaff.Dal;
using System.Windows.Forms;
using System.Data;

namespace HiStaff.iProfile
{
    public partial class Working : FormBase
    {
        public Working()
        {
            InitializeComponent();
        }

        protected override bool FormLoad()
        {
            try
            {
                //AddBasicCommandBar();
                CreateCommandBar(CommandBarManager.ActionCommand.New,
                CommandBarManager.ActionCommand.Edit,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Save,
                CommandBarManager.ActionCommand.Cancel,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Delete,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Print,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Exit);

                SetToolStripButtonBar(CommandBarManager.ActionCommand.Cancel);
                BaseStatus = Util.Enum.FormStatus.View;
                SetStatusInputControl(this);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        private void BindDataByEmp()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_EMPLOYEE>("PRS_HU_EMPLOYEE", new EMPLOYEE_SEARCH { ORGID = trvOrg.OrgId, TERMINATE = hiCheckBox1.Value });
                grdEmpList.DataSource = lstEmployee;
                grdEmpList.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void BindData()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_WORKING>("APRS_HU_WORKING", new EMPLOYEE_SEARCH { EMPLOYEEID = ConvertHelper.ToDecimal(grdEmpList.GetValue("ID")) });
                grdMain.DataSource = lstEmployee;
                grdMain.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void trvOrg_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                BindDataByEmp();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
        protected override bool PrintButtonClick()
        {
            try
            {
                string path;
                DataTable dt = new DataTable();
                dt = OracleHelper.ExcuteSelectDataTable("PRE_HU_WORKING", new EMPLOYEE_SEARCH { ID = ConvertHelper.ToDecimal(grdMain .GetValue("ID")) });
                path = (string)(dt.Rows[0]["TEMPLATE"]);
                Export.Export2WordFromDatabase (path,dt);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }
        private void grdEmpList_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (grdEmpList.GetValue("ID") == null)
                {
                    grdMain.DataSource = null;
                    grdMain.Refresh();
                    ResetInputControl(this);
                }
                else
                {
                    BindData();
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void grdMain_SelectionChanged(object sender, EventArgs e)
        {
            BindDataFromGridToForm(grdMain, this);
            this.Cursor = Cursors.WaitCursor;
            if (grdMain.SelectedItems.Count > 0)
            {
                if (grdMain.SelectedItems[0].RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    var obj = new HU_LINEMANAGER();
                    obj.WORKINGID = ((HU_WORKING)(grdMain.SelectedItems[0].GetRow().DataRow)).ID;
                    var lstLineMgt = OracleHelper.ExcuteSelectMultiObject<EMPLOYEE_SELECTION>("APRS_HU_LINEMANAGER", obj);
                    cboLineMng.EmployeeSelected = lstLineMgt;
                }
                else cboLineMng.EmployeeSelected = null;
            }
            else
            {
                cboLineMng.EmployeeSelected = null;
            }
            this.Cursor = Cursors.Default;
        }

        private void hiCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                BindDataByEmp();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        protected bool CheckData()
        {
            try
            {
                if (!Common.CheckEffectDateAndExpireDate(txtWEffectDate, txtWExpireDate))
                {
                    MessageBox.Show(GetMessageById("MSG_INVALID_DATE"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }

                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool SaveButtonClick()
        {
            try
            {
                if (!CheckData())
                {
                    return false;
                }
                else
                {
                    var objSave = new HU_WORKING();
                    ValueControlsToObject(objSave, this);
                    objSave.EMPLOYEEID = ConvertHelper.ToDecimal(grdEmpList.GetValue("ID"));
                    if (BaseStatus == Util.Enum.FormStatus.Add)
                    {
                        objSave.ID = 0;
                    }
                    OracleHelper.ExcuteNonQuery("APRU_HU_WORKING", objSave);

                    var objLineMng = new HU_LINEMANAGER();
                    objLineMng.WORKINGID = objSave.ID;
                    OracleHelper.ExcuteNonQuery("APRD_HU_LINEMANAGER", objLineMng);
                    foreach (var emp in cboLineMng.EmployeeSelected)
                    {
                        objLineMng.EMPLOYEEID = emp.ID;
                        OracleHelper.ExcuteNonQuery("APRU_HU_LINEMANAGER", objLineMng);
                    }

                    BindData();
                    MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool CancelButtonClick()
        {
            try
            {
                BindDataFromGridToForm(grdMain, this);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool DeleteButtonClick()
        {
            if (grdMain.SelectedItems.Count > 0)
            {
                var row = grdMain.SelectedItems[0];
                if (row.RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    if (OracleHelper.ExcuteNonQuery("APRD_HU_WORKING", row.GetRow().DataRow))
                    {
                        MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        BindData();
                        return true;
                    }
                }
            }
            return false;
        }


    }


}
